package com.ms.qw.application.mappers.handler;

import com.ms.qw.enums.ActivoInactivo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.hibernate.HibernateException;
import org.hibernate.annotations.TypeDef;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.CustomType;
import org.hibernate.type.StringType;
import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor;
import org.hibernate.usertype.UserType;
import org.springframework.stereotype.Component;

import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedTypes(ActivoInactivo.class)
@MappedSuperclass
@TypeDef(defaultForType = ActivoInactivo.class, typeClass = ActivoInactivoTypeHandler.class)
public class ActivoInactivoTypeHandler   implements TypeHandler<ActivoInactivo>{

    public void setParameter(PreparedStatement preparedStatement, int i, ActivoInactivo item, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, item.getCodigo());
    }

    public ActivoInactivo getResult(ResultSet resultSet, String s) throws SQLException {
        if(StringUtils.isBlank(resultSet.getString(s))) return null;
        return ActivoInactivo.findByCode(resultSet.getString(s));
    }

    public ActivoInactivo getResult(CallableStatement callableStatement, int i) throws SQLException {
        return ActivoInactivo.findByCode(callableStatement.getString(i));
    }
}
